package com.ych.modules.cms.unAuthorInfc.login.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.ych.modules.api.unAuthorInfc.login.entity.LoginVo;
import com.ych.modules.cms.unAuthorInfc.login.service.ICmsUserInfoService;
import com.ych.modules.base.dbEntity.TUserInfoEntity;
import com.ych.modules.base.dbEntity.TUserTokenEntity;
import com.ych.modules.base.service.ITUserInfoService;
import com.ych.modules.base.service.ITUserTokenService;
import com.ych.redis.RedisUtil;
import com.ych.redis.config.RedisKeyConfig;
import com.ych.utils.TLMap;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.crypto.hash.Sha256Hash;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * Author: Usopp.tsui
 * Date: 2020/12/19
 * Time: 14:31
 * Description:
 */
@Service("ICmsUserInfoService")
public class CmsUserInfoServiceImpl implements ICmsUserInfoService {
    @Autowired
    private ITUserInfoService  itUserInfoService;
    @Autowired
    private ITUserTokenService itUserTokenService;
    @Autowired
    private RedisUtil          redisUtil;

    @Override
    public Map<String, Object> login(LoginVo loginVo) {
        TUserInfoEntity userInfoEntity = this.queryByMobile(loginVo.getMobile());
        if (null == userInfoEntity) {
            throw new RuntimeException("用户名或密码不正确[N]");
        }
        if (!userInfoEntity.getPassword().equals(new Sha256Hash(loginVo.getPassword(), userInfoEntity.getSalt()).toHex())) {//新系统匹配方式
            throw new RuntimeException("用户名或密码不正确[P]");
        }
        //生成数据库token票据
        TUserTokenEntity tokenEntity = itUserTokenService.createToken(userInfoEntity.getUserId());
        if (null == tokenEntity) {
            throw new RuntimeException("无法验证用户身份");
        }
        //不将用户密码返回去
        userInfoEntity.setPassword(null);
        userInfoEntity.setSalt(null);
        TLMap.setUserId(userInfoEntity.getUserId());//将user装入当前线程内
        TLMap.setToken(tokenEntity.getToken());//将token装入当前线程内
        TLMap.setMobile(userInfoEntity.getMobile());
        Subject subject = SecurityUtils.getSubject();
        //将token交给shiro认证
        subject.login(new UsernamePasswordToken(tokenEntity.getToken(), tokenEntity.getToken()));
        Map<String, Object> map = new HashMap<>();
        map.put("token", tokenEntity.getToken());
        map.put("expire", RedisKeyConfig.expireTime);
        map.put("user", userInfoEntity);
        return map;
    }

    /**
     * 通过手机号查询
     *
     * @param mobile
     * @return
     */
    private TUserInfoEntity queryByMobile(String mobile) {
        Wrapper<TUserInfoEntity> wrapper = new EntityWrapper<TUserInfoEntity>()
                .where("1=1")
                .eq("mobile", mobile);
        TUserInfoEntity userInfoEntity = itUserInfoService.selectOne(wrapper);
        return userInfoEntity;
    }
}
